import { useAmtfStore } from 'src/stores/amtf-store';
import { storeToRefs } from 'pinia';

import { reactive, onMounted, onBeforeMount, markRaw } from 'vue'
import stores from 'src/stores';

export default function (ws) {
  onBeforeMount(() => {
    // console.log("我被执行了吗？");
    // window.ws = new WebSocket("ws://localhost:12112");
  })

  // window.ws = new WebSocket("ws://localhost:12112");

  // console.log(ws);
  const store = useAmtfStore();
  const { 排序 } = store;

  ws.onopen = function () {
    ws.send(JSON.stringify({ 'ui连接状态': 'ui已连接√' }));
  };
  ws.onmessage = function (evt) {
    // alert(evt.data)
    let kk = JSON.parse(evt.data)
    let params = null
    console.log("收到的源数据", kk);
    switch (true) {
      case 'su连接状态' in kk:
        store.su连接状态 = kk.su连接状态;
        break;
      case '主组件' in kk:
        // alert(kk)
        // console.log(kk);
        store.主组件属性 = kk.主组件;
        // console.log("store.主组件属性",store.主组件属性);
        store.父组件属性 = markRaw(kk.父组件);
        // console.log("markRaw(kk.父组件)",store.父组件属性);
        store.相关组件属性 = markRaw(kk.相关组件);
        console.log("store.相关组件属性", store.相关组件属性);


        console.log("store", store);
        排序();
        break;
      case '修改组件属性结果' in kk:
        break;
      case '调用amtf_dxf2结果' in kk:
        params=kk.调用amtf_dxf2结果
        if (params=="清空旧值") {
          store.调用amtf_dxf2结果=[];
        } else {
          store.调用amtf_dxf2结果.push(kk.调用amtf_dxf2结果);
        }
        break;
      case '接收解读后的组件名' in kk:
        params=kk.接收解读后的组件名
        let n=params.split("_");
        store.生产.成品柜名=n[0]
        store.生产.单元柜名=n[1]
        store.生产.订单=n[3]
        store.生产.客户=n[4]
        break;
      default:
        break;
    }
  };

  return { store, storeToRefs }
}
